package com.jingzhi.huimiao;

import java.util.ArrayList;

import com.jingzhi.huimiao.utils.DensityUtil;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

/**
 * 向导页面
 * 
 * @author cxk
 * 
 */
public class GuideActivity extends Activity {

	private ViewPager vp_guide;
	private Button btn_regisit;
	private Button btn_login;
	private LinearLayout ll_point_gp;
	private ImageView iv_point;
	private int ids[];
	private ArrayList<ImageView> imageviews;
	private int px;
	private ImageView iv_red_point;
	private int maxleft;// 两点间最大距离
	private double leftMargin;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// this.requestWindowFeature(Window.FEATURE_NO_TITLE);//
		// 去掉标题栏，一定要在setContentView之前
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_guide);

		vp_guide = (ViewPager) findViewById(R.id.vp_guide);
		btn_regisit = (Button) findViewById(R.id.btn_regisit);
		ll_point_gp = (LinearLayout) findViewById(R.id.ll_point_gp);
		iv_red_point = (ImageView) findViewById(R.id.iv_red_point);
		btn_login = (Button) findViewById(R.id.btn_login);

		// 设置页面监听器
		vp_guide.setOnPageChangeListener(new GuideOnPageChangeListener());
		// 给viewpager准备数据
		ids = new int[] { R.drawable.guide_01, R.drawable.guide_02 };

		// 给viewpager设置适配器
		vp_guide.setAdapter(new MyAdapter());
		// 给viewpager添加小圆点
		imageviews = new ArrayList<ImageView>();
		for (int i = 0; i < ids.length; i++) {
			ImageView imageview = new ImageView(this);
			imageview.setBackgroundResource(ids[i]);
			imageviews.add(imageview);

			// 添加移动的点
			ImageView point = new ImageView(this);
			point.setBackgroundResource(R.drawable.point_unfocused);
			// 给布局设置参数
			// 将dp转化成px
			px = DensityUtil.dip2px(GuideActivity.this, 10);
			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
					px, px);

			if (i != 0) {
				params.leftMargin = px * 2;
			}
			point.setLayoutParams(params);
			ll_point_gp.addView(point);

			/**
			 * 计算小红点需要移动的距离 View的生命周期主要的方法：
			 * onMearsure()---->onLayout()---->onDraw();
			 * 监听当onLayout()执行的时候，有个回调的方法
			 */
			iv_red_point.getViewTreeObserver().addOnGlobalLayoutListener(
					new GuideOnGlobalLayoutListener());
		}

		// 给注册按钮设置点击监听
		btn_regisit.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivity(new Intent(GuideActivity.this,
						RegistActivity.class));
				finish();
			}
		});

		// 给登录按钮设置监听
		btn_login.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivity(new Intent(GuideActivity.this,
						LoginActivity.class));
				finish();
			}
		});
	}

	class MyAdapter extends PagerAdapter {

		private View view;
		private ImageView imageView;

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return ids.length;
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			view = View.inflate(getApplicationContext(),
					R.layout.guide_vp_item, null);
			imageView = (ImageView) view.findViewById(R.id.imageView);
			imageView.setBackgroundResource(ids[position]);

			container.addView(view);
			return view;
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			// TODO Auto-generated method stub
			return arg0 == arg1;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			// TODO Auto-generated method stub
			// super.destroyItem(container, position, object);
			container.removeView((View) (object));

		}

	}

	class GuideOnGlobalLayoutListener implements OnGlobalLayoutListener {

		@Override
		public void onGlobalLayout() {
			iv_red_point.getViewTreeObserver().removeGlobalOnLayoutListener(
					this);
			// 两点间的距离 = 第一个点左边的距离 - 第二个点左边的距离
			maxleft = ll_point_gp.getChildAt(1).getLeft()
					- ll_point_gp.getChildAt(0).getLeft();

		}

	}

	class GuideOnPageChangeListener implements OnPageChangeListener {

		/**
		 * 当页面滑动的时候被回调 position：当前页面的下标 positionOffset：滑动页面的百分比
		 * positionOffsetPixels：滑动了页面多少像素
		 */
		@Override
		public void onPageScrolled(int position, float positionOffset,
				int positionOffsetPixels) {
			// 小红点的移动距离 / 两点间距离 = Viewpager页面滑动的百分比
			// 小红点的最终移动的leftMargin = 起始位置 + 小红点的移动距离
			leftMargin = position * maxleft + positionOffset * maxleft;
			RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
					px, px);
			params.leftMargin = (int) leftMargin;
			iv_red_point.setLayoutParams(params);
		}

		/**
		 * 当某个页面被选中的时候调用这个方法
		 */
		@Override
		public void onPageSelected(int position) {
			if (position == imageviews.size() - 1) {
				// 设置显示button
				btn_regisit.setVisibility(View.VISIBLE);
				btn_login.setVisibility(View.VISIBLE);
			} else {
				// 不显示button
				btn_regisit.setVisibility(View.GONE);
				btn_login.setVisibility(View.GONE);
			}
		}

		@Override
		public void onPageScrollStateChanged(int state) {
			// TODO Auto-generated method stub

		}

	}

}
